#include "containers.h"

struct stack_t *init_stack()
{
  struct stack_t *ret;

  ret = (struct stack_t *)malloc(sizeof(struct stack_t));
  memset(ret, 0, sizeof(struct stack_t));

  return ret;
}

void push_stack(struct stack_t *stack, int __data)
{
  struct data_t *data = NULL;
  if (stack->len)
  {
    // stack is not empty

    // Allocate new data_t , and point next element of first node with __data
    data = new_data(__data, stack->head);
    // move the stack head to new;
    stack->head = data;
  }
  else
  {
    // stack is empty

    data = new_data(__data, NULL);
    stack->head = data;
  }

  // increment number of elements in the stack
  stack->len++;
}

int pop_stack(struct stack_t *stack)
{
  struct data_t *tmp;
  int data;

  if (!stack->len)
    return CONTAINER_EMPTY;

  // point to the first element of stack using tmp;
  tmp = stack->head;
  // move the stack head pointer to next element that in the stack
  stack->head = tmp->next;

  // return the data and deallocate memory
  data = tmp->data;
  free(tmp);

  return data;
}
